#include "kgraphicsscene.h"

KGraphicsScene::KGraphicsScene(QObject *parent) :
    QGraphicsScene(parent), m_currDrawType(DrawToolLine),
    m_currDrawItem(NULL), perm(false)
{
}

// 鼠标点击事件
void KGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_currDrawType == DrawNone)
    { //如果是选择图形
        if(m_currDrawItem) {
            //m_currDrawItem->SelectDraw(event);
            perm = false;
        }else{

        }
    }else{
        // 根据绘制类型 添加绘制item
        switch(m_currDrawType)
        {
        case DrawToolLine: //画线
            {
                KLine *line = new KLine;
                m_currDrawItem = line;
                addItem(line);
                break;
            }
        case DrawToolRect: //画框
            {
                KRect *rect = new KRect;
                m_currDrawItem = rect;
                addItem(rect);
                break;
            }
        }
        if(m_currDrawItem) {
            m_currDrawItem->OnLButtonDown(event);
            perm = false;
        }
    }


    QGraphicsScene::mousePressEvent(event);
}

// 鼠标移动事件
void KGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_currDrawItem && !perm) {
        if(m_currDrawType == DrawNone)
        { //如果是移动图形
            //m_currDrawItem->PositionMove(event);
        }else{
            m_currDrawItem->OnMouseMove(event);
        }
    }
    QGraphicsScene::mouseMoveEvent(event);
}

// 鼠标释放事件
void KGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    perm = true;

    if(m_currDrawItem) {
        m_currDrawItem->OnLButtonUp(event);
    }

    m_currDrawType = DrawNone;
    QGraphicsScene::mouseReleaseEvent(event);
}
